﻿Public Class ColorSelection : Inherits ToolStrip

	Sub New()
		Renderer = New Renderer
		AutoSize = False
		Padding = Padding.Empty
		Margin = Padding.Empty
		ImageScalingSize = New Size(15, 15)
		Size = New Size(12 * (ImageScalingSize.Width + 4), 8 * (ImageScalingSize.Height + 4))
		MinimumSize = Size
		MaximumSize = Size
		LayoutStyle = ToolStripLayoutStyle.Flow
		For y = 0 To 7
			For x = 0 To 11
				Dim r = Mid("444200000244", x + 1, 1) * 63
				Dim g = Mid("024444420000", x + 1, 1) * 63
				Dim b = Mid("000002444442", x + 1, 1) * 63
				Dim col = Color.FromArgb(r, g, b)
				Dim col1 As Color
				If y = 0 Then col1 = Color.Black.Mix(Color.White, 100 * x / 11)
				If y > 0 Then col1 = col.Mix(If(y < 5, Color.White, Color.Black), Choose(y, 0, 75, 50, 25, 25, 50, 75))
				Dim Button As New ToolStripButton
				Dim Bm As New Bitmap(ImageScalingSize.Width, ImageScalingSize.Height)
				Dim gr = Graphics.FromImage(Bm)
				gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
				Dim Rect As New Rectangle(0, 0, Bm.Width, Bm.Height)
				Rect.Inflate(-1, -1)
				gr.FillRoundRect(col1.Brush, Rect, 3)
				gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
				Rect.Width -= 1 : Rect.Height -= 1
				gr.DrawRoundRect(Color.Black.Opacity(64).Pen, Rect, 2)
				Button.Image = Bm
				Button.AutoSize = False
				Button.Size = New Size(ImageScalingSize.Width + 4, ImageScalingSize.Height + 4)
				Button.Padding = Padding.Empty
				Button.Margin = Padding.Empty
				Button.Tag = col1
				'If y = 0 Or y = 1 Then Button.Margin = New Padding(0, 0, 0, 4)
				Items.Add(Button)
			Next
		Next
	End Sub

	Event ColorClicked(col As Color)

	Protected Overrides Sub OnItemClicked(e As System.Windows.Forms.ToolStripItemClickedEventArgs)
		MyBase.OnItemClicked(e)
		RaiseEvent ColorClicked(CType(e.ClickedItem.Tag, Color))
	End Sub

End Class